import Vue from 'vue'
import VueRouter from "vue-router";
import Login from "../components/Login";
import Register from "../components/Register";
import Userhome from "../components/User/userHome.vue";
import Menu from "../components/User/Menu.vue";
import Cart from "../components/User/ShoppingCart.vue";
import Orders from "../components/User/MyOrder.vue";
import Profile from "../components/User/ModifyInformation.vue";
import Payment from "../components/User/payment.vue";
import Charge from "../components/User/Charge.vue";
import AdminHome from "@/components/Admin/AdminHome.vue";
import ProductManager from "@/components/Admin/ProductManager.vue";
import UserManager from "@/components/Admin/UserManager.vue";
import ProductTypeManager from "@/components/Admin/ProductTypeManager.vue";
import OrderManager from "@/components/Admin/OrderManager.vue";
import message from "@/utils/message";
import {currentUser} from "@/API/UserAPI";
import async from "async";
import PaySuccess from "@/components/User/PaySuccess.vue";

Vue.use(VueRouter)

const router1 = new VueRouter({
    routes: [
        {
            path: '/',
            name: 'Index',
            redirect: {name: "Login"}
        },
        {
            name: 'Login',
            path: '/login',
            component: Login
        },
        {
            name: 'Register',
            path: '/register',
            component: Register
        },
        {
            name: 'Payment',
            path: '/payment:orderId',
            component: Payment
        },
        {
            name: 'PaySuccess',
            path: '/payok',
            component: PaySuccess
        },
        {
            name: 'Userhome',
            path: '/userhome',
            component: Userhome,
            children: [ //通过children配置子级路由
                {
                    path: 'Orders',
                    component: Orders
                },
                {
                    path: 'Cart',
                    component: Cart
                },
                {
                    path: 'Menu', //此处一定不要写：/news
                    component: Menu
                },
                {
                    path: 'Profile', //此处一定不要写：/news
                    component: Profile
                },
                {
                    path: 'Charge',
                    component: Charge
                },

            ]
        },
        {
            name: 'AdminHome',
            path: '/adminHome',
            component: AdminHome,
            children: [
                {
                    path: 'products',
                    component: ProductManager
                },
                {
                    path: 'users',
                    component: UserManager
                },
                {
                    path: 'productTypes',
                    component: ProductTypeManager
                },
                {
                    path: 'orders',
                    component: OrderManager
                },
            ]

        },

    ]

})
// router1.beforeEach((to, from, next) => {
//     // 1. 判断是不是登录页面
//     // 是登录页面
//     if (to.path === '/login' || to.path === '/register' || to.path === '/userhome/Menu' || to.path === '/userhome') {
//         next()
//     } else {
//         // 不是登录页面
//         // 2. 判断 是否管理员操作
//         let token = localStorage.getItem('token')
//         if (token) {
//             next();
//         }
//         next('/login');
//
//     }
// })

export default router1